package com.example.travelapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.Room;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.example.travelapplication.R;
import com.example.travelapplication.dao.AttDao;
import com.example.travelapplication.database.TravelDatabase;
import com.example.travelapplication.entity.Attraction;
import com.example.travelapplication.entity.Favor;

import java.util.List;

public class FavorAdapter extends RecyclerView.Adapter<FavorAdapter.ViewHolder>{

    private List<Favor> data;
    private Context context;
    private OnItemClickListener listener;
    private TravelDatabase travelDatabase;
    private AttDao attDao;

    public FavorAdapter(List<Favor> data, Context context, OnItemClickListener listener) {
        this.data = data;
        this.context = context;
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewholder_attraction, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FavorAdapter.ViewHolder holder, int position) {
        travelDatabase = Room.databaseBuilder(context, TravelDatabase.class, "travelDb")
                .allowMainThreadQueries()
                .build();
        attDao = travelDatabase.attDao();

        Attraction attraction = attDao.getAttById(data.get(position).attId);
        holder.textViewTitle.setText(attraction.title);
        holder.textViewLocation.setText(attraction.location);
        holder.textViewScore.setText(String.valueOf(attraction.score));
        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.drawable.loading)
                .error(R.drawable.error)
                .centerCrop();
        Glide.with(context)
                .load(attraction.pic)
                .apply(requestOptions)
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(holder.imageView);

        travelDatabase.close();
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setData(List<Favor> newData) {
        this.data = newData;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView textViewTitle;
        TextView textViewLocation;
        TextView textViewScore;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemClick(getAdapterPosition());
                    }
                }
            });
            imageView = itemView.findViewById(R.id.att_pic);
            textViewTitle = itemView.findViewById(R.id.att_title);
            textViewLocation = itemView.findViewById(R.id.att_loc);
            textViewScore = itemView.findViewById(R.id.att_sco);
        }
    }
}
